﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using CustomsSystem.BLL;

namespace WEB.T_bill
{
    public partial class XiaoZhangYeMian : System.Web.UI.Page
    {
        CustomsSystem.BLL.UserManage UserManage = new UserManage();
        int sheetID = 0;
        string userName = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtXiaoZhangDate.Attributes.Add("onClick", "new WdatePicker()");
            

            if (!IsPostBack)
            {
                txtXiaoZhangDate.Text = DateTime.Now.ToShortDateString().ToString();

                //绑定收款人
                DataTable dt=GetEmpNum();
                ddlEmpID.DataSource = dt;
                ddlEmpID.DataValueField = dt.Columns[2].ColumnName;
                ddlEmpID.DataTextField = dt.Columns[2].ColumnName;
                ddlEmpID.DataBind();
                ddlEmpID.Items.Remove("admin");
                
                ddlEmpID.Text = Session["UserName"].ToString();//默认是当前登录用户
                userName = ddlEmpID.Text;
            }
            sheetID = int.Parse(Request.QueryString["ID"]);//获得委托单查看页面传过来的sheetID
        }


        /// <summary>
        /// 获取人员表
        /// </summary>
        /// <returns></returns>
        public DataTable GetEmpNum()
        {
            DataSet ds = UserManage.GetAllList();
            DataTable dt = ds.Tables[0];
            return dt;
        }


        //点击销账按钮后
        protected void btnXiaoZhang_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            int billTypeID = 0;

            CustomsSystem.BLL.BillManage billBLL = new BillManage();
            CustomsSystem.Model.Bill billModel = new CustomsSystem.Model.Bill();
            CustomsSystem.BLL.PayManage payBLL = new PayManage();
            CustomsSystem.Model.Pay payModel = new CustomsSystem.Model.Pay();

            //根据委托单号获得应收应付号
            //int billID = Convert.ToInt32(billBLL.GetSingle(sheetID.ToString()));
            //billModel = billBLL.GetModel(billID);

            if(Session["yingShou"]!=null)
            {
                if (Session["yingShou"].ToString() == "应收销账")
                {
                    billTypeID = 1;
                }
            }
            else if (Session["yingFu"].ToString() == "应付销账")
            {
                billTypeID = 2;
            }

            ds = billBLL.getBillID(sheetID, billTypeID);

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                int billID = Convert.ToInt32(ds.Tables[0].Rows[i][0]);//获得T_bill表中的billID号
                billModel = billBLL.GetModel(billID);
                //把T_bill中的数据存到T_pay表中
                payModel.billID = billModel.billID;
                payModel.sheetID = billModel.sheetID;
                payModel.cID = billModel.cID;
                payModel.feeTypeID = billModel.feeTypeID;
                payModel.sfeeAmount = billModel.sfeeAmount;

                //判断查看页面单击的是应收销账按钮还是应付销账按钮
                if (Session["yingShou"] != null)
                {
                    if (Session["yingShou"].ToString() == "应收销账")
                    {
                        payModel.payTypeID = 1;
                    }
                }
                else if (Session["yingFu"].ToString() == "应付销账")
                {
                    payModel.payTypeID = 2;
                }

                //根据用户名去Cu_User里拿相应的用户ID
                userName = ddlEmpID.SelectedItem.Text;
                CustomsSystem.BLL.UserManage userBLL = new CustomsSystem.BLL.UserManage();
                string empNum = userBLL.GetSingle(userName).ToString();
                payModel.empID = empNum;

                payModel.payDate = Convert.ToDateTime(txtXiaoZhangDate.Text);
                payModel.payType = ddlPayType.SelectedIndex + 1;

                int result = payBLL.Add(payModel);

                //if (result != 0)//记录已经增加到T_Pay表中
                //{
                    
                //}
            }
            billBLL.updatePayedID(sheetID, billTypeID);
            Response.Redirect("xiaoZhang.aspx");//跳转回委托单查看页面
        }
    }
}